$(function() {
    function h() {
        $(".img-container").hasClass("edit") && b.cropper("setData", l)
    }
    $("#myTab a").click(function(a) {
        a.preventDefault();
        $(this).tab("show")
    });
    $("#settings div.btn-default").on("click", function() {
        var a = $(".v-site-name").val(),
            c = $(".v-site-pass").val();
        $.ajax({
            type: "POST",
            url: "actions.php",
            data: "site_name=" + a + "&site_pass=" + c + "&action=changeSiteInfo",
            cache: !1,
            success: function(a) {
                alert(a);
                window.location.href = "?tab=settings";
                $(document).ready(function(a) {
                    $(".nav-tabs li:last").click()
                })
            }
        })
    });
   
    
    //注销登录
    $(".log-out").on("click", function() {
        $.ajax({
            type: "POST",
            url: "actions.php",
            data: "action=logout",
            cache: !1,
            success: function() {
                history.go(0)
            }
        })
    });
    //删除
    $(".manage-user").on("click", ".user-delete", function() {
        var mymessage = confirm("确定要删除吗?");
        if (mymessage === true) {
            $(this).parents('tr').remove();
        }
    });
    /*---------------------组别管理---------------------*/
    // 编辑
    $(".manage-user").on("click", ".user-edit", function() {
        var $modal = $('#myModa0');
        $modal.attr('data-type', 'editor');
        // 获取数据
        var $tr = $(this).parents('tr');
        $(this).parents('tr').addClass('editor');
        var name1 = $tr.find('td').eq(0).text();
        var name2 = $tr.find('td').eq(1).text();
        $modal.find('.name1').val(name1);
        $modal.find('.name2').val(name2);
    });
    //新增
    $('#btn-group-add').on("click", function() {
        var $modal = $('#myModa0');
        $modal.attr('data-type', 'add');
        $modal.find(".name1").val('');
        $modal.find(".name2").val('');
    });
    //模态框确定
    $("#addzubie").on("click", function() {
        // var $(this).parents('.modal')
        var $modal = $('#myModa0');
        var name1 = $(".name1").val();
        var name2 = $(".name2").val();
        // console.log($(this).parents('.modal').data('type'));
        var modalType = $(this).parents('.modal').attr('data-type');
        if (modalType === "editor") {
            var $tr = $('.manage-user').find('tr.editor');
            $tr.find('td').eq(0).text(name1);
            $tr.find('td').eq(1).text(name2);
            $tr.removeClass('editor');
            $modal.find('.close').trigger('click');
        } else if (modalType === "add") {
            var newzubie = "";
            newzubie = '<tr><td>' + name1 + '</td><td>' + name2 + '</td><td><a class="label label-primary user-edit" data-toggle="modal" data-target="#myModa0"> 编辑</a>  <a class="label label-primary user-delete">删除</a>  <a class="label label-danger user-choose">选择人员</a></td></tr>';
            $('#home table').append(newzubie);
            $modal.find('.close').trigger('click');
        }
    });
 
    $("#buttonUpload").click(function() {
        b.data("cropper");
        $.ajaxFileUpload({
            url: "upd.php",
            secureuri: !1,
            fileElementId: "fileToUpload",
            dataType: "json",
            success: function(a, c) {
                "undefined" !=
                typeof a.error && ("" != a.error ? alert(a.error) : b.cropper("setImgSrc", a.msg))
            },
            error: function(a, c, f) {
                alert(f)
            }
        });
        return !1
    });
    $("#set-img-src").click(function() {
        b.data("cropper");
        var a = $("#set-img-src-input").val();
        b.cropper("setImgSrc", a)
    });
    (function(a, c) {
        $(a).each(function() {
            var a = $(this),
                b = a.children("tbody").children("tr").length - 1,
                b = 0 == b % c ? parseInt(b / c) + 1 : b / c,
                d = a.children("tbody").children("tr").eq(0).clone();
            a.parent().append('<ul class="pagination"></ul>');
            a.siblings(".pagination").empty();
            for (var e =
                    0; e < b; e++) a.siblings(".pagination").append('<li><a href="#">' + (e + 1) + "</a></li>"), a.append("<tbody></tbody>"), d.clone().appendTo(a.children("tbody").eq(e + 1)), a.children("tbody:eq(0)").children("tr").slice(1, 16).appendTo(a.children("tbody").eq(e + 1));
            a.children("tbody").eq(0).remove();
            b = a.children("tbody").eq(0);
            a.children("tbody").hide();
            b.show();
            a.siblings(".pagination").children("li:first").addClass("active");
            a.siblings(".pagination").find("a").on("click", function() {
                var b = $(this).parent().index();
                a.children("tbody").hide().eq(b).show();
                $(this).parent().addClass("active").siblings().removeClass("active");
                return !1
            })
        })
    })(".manage-user", 15);
    var b = $(".img-container img"),
        d = $(".img-container");
    $("#data-x1");
    $("#data-y1");
    $("#data-x2");
    $("#data-y2");
    $("#data-height");
    $("#data-width");
    b.cropper({
        aspectRatio: .8,
        preview: ".img-preview",
        done: function(a) {
            d.attr("x1", a.x1);
            d.attr("y1", a.y1);
            d.attr("h", a.height);
            d.attr("w", a.width)
        }
    });
    var m = $("#drag-move");
    b.on("dragmove", function() {
        m.addClass("btn-info").siblings().removeClass("btn-info")
    });
    var l = {
        x1: 0,
        y1: 0,
        width: 208,
        height: 260
    };
    $("#enable").click(function() {
        b.cropper("enable")
    });
    var k = new Image,
        n = $(".img-container img").attr("src");
    k.onload = function() {
        h()
    };
    k.src = n;
    $(".reset").on("click", function() {
        h()
    });
    $(".search").click(function() {
        $("#profile .manage-user tbody").show();
        $("#profile .manage-user tr").hide().filter(":contains('" + $(this).val() + "')").show()
    });
    
});
